﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace UserInterface
{
    public enum LanePosition { Middle, Start, End};

    [Serializable()]
   public class PedLane : Lane
    {
        private List<Pedestrian> pedestrian;
        //public Image img = 
        public Direction direction { get; set; }
        public LanePosition position { get; set; }
        public int pedNum { get; set; }
        public PedLane destination { get; set; }
        public Sensor sensor;
        public bool activeSensor
        {
            get;
            set;

            //get {return sensor == null ? false : true;}
            //set
            //{
            //    if (value)
            //    {
            //        activeSensor = true;
            //        if (sensor == null)
            //            sensor = new Sensor();
            //    }
            //    else
            //        if (activeSensor)
            //        {
            //            sensor = null;
            //            activeSensor = false;
            //        }
            //}
        }

        public PedLane(string id, Point start, Point end, Direction dir, LanePosition lnp = LanePosition.Middle) 
        {
            this.LaneId = id;
            this.start = start;
            this.end= end;
            this.img = Rsrc_Images.pedestrians;
            this.direction = dir;
            this.position = lnp;
            this.pedNum = 0;
            this.pedestrian = new List<Pedestrian>();
            activeSensor = false;
            MAXCARS = 5;
        }

        public void addPedestrian(Pedestrian p)
        {
            p.departure = this;
            p.pos = getGapOfPedo(p);
            this.pedestrian.Add(getGapOfLane(p));
            if (speed != -1)
                pedestrian[pedestrian.Count - 1].speed = speed;
            this.pedNum++;
        }

        public void removePedestrian()
        {
            if (pedestrian.Count > 0)
            {
                this.pedestrian.RemoveAt(0);
                this.pedNum--;
            }
        }

        public void addDestination(PedLane dest)
        {
            this.destination = dest;
        }

       public Pedestrian GetPedestrianfromList(int j)
       {
           return pedestrian[j];
       }

       public override void Move(object s, EventArgs e)
       {
           
           for (int i = 0; i < pedestrian.Count; i++)
           {
               if (i == 0)
               {         
                                      // timer.Enabled
                   if (LaneId == "NRight" || LaneId == "NLeft" || LaneId == "SRight" || LaneId == "SLeft")
                        pedestrian[0].MoveOnPedLane(this.LaneId, this.end , canMove);
                   else
                       pedestrian[0].MoveOnPedLane(this.LaneId, this.end , true);
               }
               else
               {                                                           // always false
                   pedestrian[i].MoveOnPedLane(this.LaneId, getGapOfPedo(pedestrian[i-1]), false);
               }
           }
       }

       public List<Pedestrian> getPedestrians()
       {
           return pedestrian;
       }

       public Pedestrian getGapOfLane(Pedestrian p)
       {
           switch (this.LaneId)
           {
               case "NLeft":
               case "NLeft-Right1":
               case "NLeftExit":

               case "SLeft":
               case "SLeft-Right1":
               case "SLeftExit":
                   p.pos.X -= 15;
                   break;


               case "NRight":
               case "NRight-Left1":
               case "NRightExit":

               case "SRight":
               case "SRight-Left1":
               case "SRightExit":
                   p.pos.X += 15;
                   break;
           }
           return p;
       }
      
       public Point getGapOfPedo(Pedestrian p)
       {
           Point newPoint = p.pos;
           switch (this.LaneId)
           {
               case "NLeft":
               case "NLeft-Right1":
               case "NLeftExit":

               case "SLeft":
               case "SLeft-Right1":
               case "SLeftExit":
                   newPoint.X -= 15;
                   break;


               case "NRight":
               case "NRight-Left1":
               case "NRightExit":

               case "SRight":
               case "SRight-Left1":
               case "SRightExit":
                   newPoint.X += 15;
                   break;                   
           }
           return newPoint;
       }

        public override void ResetLane()
        {
            this.pedestrian = new List<Pedestrian>();
            this.pedNum = 0;
            this.NumberOfPedos = 0;
            this.NumberOfCars = 0;
        }

    }
}
